import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { DeleteResult, Repository, UpdateResult } from 'typeorm';
import { Bk_News_Category } from './bk_news_category.entity';

@Injectable()
export class NewsCategoryService {
    constructor(
        @InjectRepository(Bk_News_Category)
        private readonly newsCategoryRepository: Repository<Bk_News_Category>
    ) { }

    async add(category: { category_name: string }): Promise<Bk_News_Category> {
        return await this.newsCategoryRepository.save(category);
    }

    async delete(id: number): Promise<DeleteResult> {
        return await this.newsCategoryRepository.delete(id);
    }

    async update(id: number, category: { category_name: string }): Promise<UpdateResult> {
        return await this.newsCategoryRepository.update(id, category);
    }

    async findAll(query: any): Promise<[Bk_News_Category[], number]> {
        return await this.newsCategoryRepository.findAndCount({ relations: ["news"], order: { id: "ASC" }, skip: query.skip, take: query.take });
    }
}
